package sf.view;

import java.awt.Component;
import java.awt.FlowLayout;

import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JToolBar;

import org.jdesktop.xswingx.JXSearchField;
import org.jdesktop.xswingx.JXSearchField.LayoutStyle;

import sf.actions.SFAbstractAction;
import sf.actions.SFActionManager;

public class Toolbar extends JToolBar {

	private static final long serialVersionUID = 1L;

	private StandardForm parentForm;

	private SFActionManager actionManager;
	
	private JXSearchField searchField;

	public Toolbar(StandardForm parentForm) {
		this.parentForm = parentForm;
		actionManager = parentForm.getActionManager();

		init();
	}

	private void init() {
		setRollover(true);

		if ((parentForm.getContext().getMode() & StandardForm.FROM_ZOOM) != 0)
			add(actionManager.getZoomOutAction());

		add(actionManager.getSearchAction());
		add(actionManager.getRefreshAction());
		add(actionManager.getHelpAction());
		addSeparator();
		add(actionManager.getUnosAction());
		// add(actionManager.getIzmenaAction());
		add(actionManager.getBrisanjeAction());
		addSeparator();
		add(actionManager.getSelectFirstAction());
		add(actionManager.getSelectPreviousAction());
		add(actionManager.getSelectNextAction());
		add(actionManager.getSelectLastAction());
		addSeparator();
		add(actionManager.getNextAction());

		JPanel p = new JPanel(new FlowLayout(FlowLayout.RIGHT));
		searchField = new JXSearchField("Brza pretraga");
		searchField.setColumns(10);
		searchField.setLayoutStyle(LayoutStyle.MAC);
		searchField.addActionListener(parentForm.getController()
				.getQuickFilterListener());
		p.add(searchField);
		add(p);

		for (Component c : getComponents())
			if (c instanceof JButton) {
				JButton button = (JButton) c;
				button.setHideActionText(false);
				button.setFocusPainted(false);
			}
	}
	
	public JXSearchField getSearchField() {
		return searchField;
	}

	public void setupButtonModes() {
		for (AbstractAction action : actionManager.getActions())
			((SFAbstractAction) action).setButtonMode(parentForm.getMode());
	}
	
	public void setupSearchField() {
		searchField.setEnabled((parentForm.getMode()&StandardForm.MODE_SEARCH) == 0);
	}
}
